package com.insigma.business.qrcode.common.vo;


import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.concurrent.atomic.AtomicLong;

public class SequenceRange {
    private String name;
    private long min;
    private long max;
    private long step;
    private long value;
    private long latestValue;
    private String tenantCode = "default_tenant_code";
    private AtomicLong localValue = new AtomicLong(0L);
    private volatile boolean over = false;

    public SequenceRange() {
    }

    public boolean isOver() {
        if (this.over) {
            return this.over;
        } else {
            return this.localValue.get() >= this.getStep() + this.value || this.localValue.get() > this.getMax();
        }
    }

    public long getNextValue() {
        long currentValue = this.localValue.getAndIncrement();
        if (currentValue > this.max) {
            this.over = true;
        }

        return currentValue;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getMin() {
        return this.min;
    }

    public void setMin(long min) {
        this.min = min;
    }

    public long getMax() {
        return this.max;
    }

    public void setMax(long max) {
        this.max = max;
    }

    public long getStep() {
        return this.step;
    }

    public void setStep(long step) {
        this.step = step;
    }

    public String getTenantCode() {
        return this.tenantCode;
    }

    public void setTenantCode(String tenantCode) {
        this.tenantCode = tenantCode;
    }

    public long getValue() {
        return this.value;
    }

    public void setValue(long value) {
        AtomicLong var3 = this.localValue;
        synchronized(this.localValue) {
            this.value = value;
            this.localValue.set(value);
        }
    }

    public long getLatestValue() {
        return this.latestValue;
    }

    public void setLatestValue(long latestValue) {
        this.latestValue = latestValue;
    }

    public void setLocalValue(AtomicLong localValue) {
        this.localValue = localValue;
    }
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + (int)(this.max ^ this.max >>> 32);
        result = 31 * result + (int)(this.min ^ this.min >>> 32);
        result = 31 * result + (this.over ? 1231 : 1237);
        result = 31 * result + (this.localValue == null ? 0 : this.localValue.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (this.getClass() != obj.getClass()) {
            return false;
        } else {
            SequenceRange other = (SequenceRange)obj;
            if (!this.name.equals(other.name)) {
                return false;
            } else if (this.max != other.max) {
                return false;
            } else if (this.min != other.min) {
                return false;
            } else if (this.over != other.over) {
                return false;
            } else {
                if (this.localValue == null) {
                    if (other.localValue != null) {
                        return false;
                    }
                } else if (!this.localValue.equals(other.localValue)) {
                    return false;
                }

                return true;
            }
        }
    }
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}
